﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Cafe.BusinessLogic;
using Cafe.Presentation;
using Cafe.DataObjectTransfer;

namespace Cafe
{
    public partial class CategoriesForm : DevExpress.XtraEditors.XtraForm
    {
        CafeManagementService _cafeService = Program.cafeService;

        public CategoriesForm()
        {
            InitializeComponent();
        }

        private void frmCategories_Load(object sender, EventArgs e)
        {
            dgvCatalogue.DataSource = _cafeService.GetCatalogues();
            dgvProduct.DataSource = _cafeService.GetProducts();
            dgvDesk.DataSource = _cafeService.GetDesks();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            switch (tcCategories.SelectedTabPageIndex)
            {
                case 0:
                    using (EditCatalogueForm form = new EditCatalogueForm())
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                _cafeService.AddCatalogue(form.CatalogueName, form.Description);
                                dgvCatalogue.DataSource = _cafeService.GetCatalogues();
                            }
                            catch (NotAuthorizedException)
                            {
                                WarnNotAuthorized();
                            }
                            catch (Exception ex)
                            {
                                XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;
                case 1:
                    using (EditProductForm form = new EditProductForm())
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                _cafeService.AddProduct(form.ProductName,form.Description,form.CatalogueID,form.UnitID,form.DateStart,form.DateEnd,form.Price,form.PromoPrice);
                                dgvCatalogue.DataSource = _cafeService.GetProducts();
                            }
                            catch (NotAuthorizedException)
                            {
                                WarnNotAuthorized();
                            }
                            catch (Exception ex)
                            {
                                XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;
                case 3:
                    using (EditDeskForm form = new EditDeskForm())
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                _cafeService.AddDesk(form.DeskName, form.Description);
                                dgvCatalogue.DataSource = _cafeService.GetDesks();
                            }
                            catch (NotAuthorizedException)
                            {
                                WarnNotAuthorized();
                            }
                            catch (Exception ex)
                            {
                                XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;
            }
        }

        private static void WarnNotAuthorized()
        {
            XtraMessageBox.Show("Bạn không có quyền truy cập chức năng này!", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(tcCategories.SelectedTabPageIndex + "");
            switch (tcCategories.SelectedTabPageIndex)
            {
                    
                case 0:
                    DataGridViewRow row = dgvCatalogue.SelectedRows[0];
                    
                    using (EditCatalogueForm form = new EditCatalogueForm() { isEdit=true,CatalogueName=row.Cells["cCCatalogueName"].Value.ToString(),Description=row.Cells["cCDescription"].Value.ToString()})
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                _cafeService.UpdateCatalogue(Convert.ToInt32(row.Cells["cCCatalogueID"].Value),form.CatalogueName,form.Description);
                                dgvCatalogue.DataSource = _cafeService.GetCatalogues();
                            }
                            catch (NotAuthorizedException)
                            {
                                WarnNotAuthorized();
                            }
                            catch (Exception ex)
                            {
                                XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;
                case 1:
                    row = dgvProduct.SelectedRows[0];

                    using (EditProductForm form = new EditProductForm() { 
                        isEdit = true, 
                        ProductName = row.Cells["cPProductID"].Value.ToString(),
                        Description = row.Cells["cPDescription"].Value.ToString(),
                        CatalogueID = Convert.ToInt32(row.Cells["cPCatalogueID"].Value),
                        UnitID = Convert.ToInt32(row.Cells["cPUnitID"].Value),
                        Price = Convert.ToDecimal(row.Cells["cPPrice"].Value),
                        PromoPrice = Convert.ToDecimal(row.Cells["cPPromoPrice"].Value),
                        DateStart = Convert.ToDateTime(row.Cells["cPDateStart"].Value),
                        DateEnd = Convert.ToDateTime(row.Cells["cPDateEnd"].Value),

                    })
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                _cafeService.UpdateProduct(
                                    Convert.ToInt32(row.Cells["cPProductID"].Value),
                                    form.ProductName,
                                    form.Description,
                                    form.CatalogueID,
                                    form.UnitID,
                                    form.DateStart,
                                    form.DateEnd,
                                    form.Price,
                                    form.PromoPrice
                                    );
                                dgvCatalogue.DataSource = _cafeService.GetCatalogues();
                            }
                            catch (NotAuthorizedException)
                            {
                                WarnNotAuthorized();
                            }
                            catch (Exception ex)
                            {
                                XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;
                case 3:

                    row = dgvDesk.SelectedRows[0];

                    using (EditCatalogueForm form = new EditCatalogueForm() { isEdit = true, CatalogueName = row.Cells["cDDeskName"].Value.ToString(), Description = row.Cells["cDDescription"].Value.ToString() })
                    {
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                dgvDesk.DataSource = _cafeService.GetDesks();
                            }
                            catch (NotAuthorizedException)
                            {
                                WarnNotAuthorized();
                            }
                            catch (Exception ex)
                            {
                                XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    break;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Xóa bản ghi hiện tại?", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                switch (tcCategories.SelectedTabPageIndex)
                {
                    case 0:
                        DataGridViewRow row = dgvCatalogue.SelectedRows[0];
                        try
                        {
                            _cafeService.DeleteCatalogue(Convert.ToInt32(row.Cells["cCCatalogueID"].Value));
                            dgvCatalogue.DataSource = _cafeService.GetCatalogues();
                        }
                        catch (NotAuthorizedException)
                        {
                            WarnNotAuthorized();
                        }
                        catch (Exception ex)
                        {
                            XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;
                    case 1:
                        row = dgvProduct.SelectedRows[0];
                        try
                        {
                            _cafeService.DeleteProduct(Convert.ToInt32(row.Cells["cPProductID"].Value));
                            dgvProduct.DataSource = _cafeService.GetDesks();
                        }
                        catch (NotAuthorizedException)
                        {
                            WarnNotAuthorized();
                        }
                        catch (Exception ex)
                        {
                            XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;
                    case 3:
                        row = dgvDesk.SelectedRows[0];
                        try
                        {
                            _cafeService.DeleteDesk(Convert.ToInt32(row.Cells["cDDeskID"].Value));
                            dgvDesk.DataSource = _cafeService.GetDesks();
                        }
                        catch (NotAuthorizedException)
                        {
                            WarnNotAuthorized();
                        }
                        catch (Exception ex)
                        {
                            XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;
                }
            }
        }
    }
}